<?php
$vars = parse_ini_file("../ini/settings.ini");
$connect = mysql_connect($vars["mysql_host"],$vars["mysql_username"],$vars["mysql_password"]) or die(mysql_error());
$db = mysql_select_db($vars["mysql_database"],$connect);
?>
<html>
<head>
<style type="text/css">
table.pretty {
	border-width: 1px;
	border-spacing: 2px;
	border-style: solid;
	border-color: black;
	border-collapse: collapse;
	background-color: white;
}
table.pretty th {
	border-width: 1px;
	padding: 1px;
	border-style: inset;
	border-color: gray;
	background-color: white;
	-moz-border-radius: ;
}
table.pretty td {
	border-width: 1px;
	padding: 1px;
	border-style: inset;
	border-color: gray;
	-moz-border-radius: ;
}
table.pretty tr:hover {
	background-color: #ccc;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
	function lookupShow(new_show,id) {
		var title = document.getElementById(id).value;
		var exact=0;
		
		if (document.getElementById('exact').checked) {
			exact=1;
		}
		if (title == "") {
			$('#results').html("");
		}
		$.post('getresults.php',{ title: title, new: new_show,exact: exact }, function(data) {
			$('#results').html(data);
		});
	}
	function Record(title,display_name,channel,call_sign) {
		$.post('recordseries.php',{ title: title,display_name:display_name,channel: channel,call_sign: call_sign }, function(data) {
			alert(data);
			window.location.reload();
		});
	}
	function RecordSingle(start,end,channel,recording_subtitle,recording_title,episode_num_dd_progid,call_sign,episode_num_onscreen) {
		$.post('recordsingle.php',{ start: start,end: end,channel: channel,recording_subtitle: recording_subtitle,recording_title: recording_title,episode_num_dd_progid: episode_num_dd_progid,call_sign: call_sign,episode_num_onscreen: episode_num_onscreen }, function(data) {
			alert(data);
			window.location.reload();
		});
		//alert("Start: " + start + " End: " + end + " Channel: " + channel + " RS: " + recording_subtitle + " RT: " + recording_title + " enddp:  " + episode_num_dd_progid + " Call Sign: " + call_sign + " ENOS: " + episode_num_onscreen);
	}
	function tvdbit(name,airdate,id) {
		if (confirm("Are you sure you wish to update this show: " + name)) {
			$.post('tvdbshow.php',{ name: name,airdate:airdate,id:id,gd:0,hasd:0,getlink:0 }, function(data) {
				alert(data);
				window.location.reload();
			});
		}
	}
	function tvdbdesc(name,airdate,id) {
		$.post('tvdbshow.php',{ name: name,airdate:airdate,id:id,gd:1,hasd:0,getlink:0 }, function(data) {
			alert(data);
			window.location.reload();
		});
	}

	function deleteRecording(id) {
		$.post('deleterecording.php',{ id: id }, function(data) {
			alert(data);
			window.location.reload();
		});
	}
	function notvdb(id) {
		$.post('notvdb.php',{ id: id }, function(data) {
			alert(data);
			window.location.reload();
		});
	}
	function changeName(id,name) {
		new_name=prompt("Change The Name",name);
		if (new_name) {
                	$.post('changename.php',{ id: id,name:new_name }, function(data) {
                        	alert(data);
                        	window.location.reload();
                	});			
		} else {
			alert("The name remains unchanged");
		}
	}
	function changeStart(id,start) {
		new_start=prompt("Change The Start Time",start);
		if (new_start) {
                	$.post('changestart.php',{ id: id,start:new_start }, function(data) {
                        	alert(data);
                        	window.location.reload();
                	});			
		} else {
			alert("The time remains unchanged");
		}
	}
	function makeAuto(id) {
		if (confirm("Are you sure you wish to remove manual fields (Everything will be overwritten when addnew.php is run)")) {
			$.post('removemanual.php',{ id: id }, function(data) {
				alert(data);
				window.location.reload();
			});
		}
	}
	function hasd(name,airdate,id,span) {
		$('#'+span).html('<img src="ajax-loader.gif" />');
		$.post('tvdbshow.php',{ name: name,airdate:airdate,id:id,gd:0,hasd:1,getlink:0 }, function(data) {
			//alert(data);
			//window.location.reload();
			$('#'+span).html(data);

		});
	}
	function deleteShow(name,airdate,id,span) {
		if (confirm("Are you sure you wish to remove this show from recording (it may come back if it's a seson recording)")) {
			$.post('deleteshow.php',{ id: id }, function(data) {
				alert(data);
				window.location.reload();
			});
		}
	}
	function gotopage(name,airdate,id) {
		$.post('tvdbshow.php',{ name: name,airdate:airdate,id:id,gd:0,hasd:0,getlink:1 }, function(data) {
			if (confirm("Would you like to open this in a new window?")) {
				//alert(data);
				//window.location.reload();
				//$('#'+span).html(data);
				window.open(data);
			}

		});
	}

</script>
<title>Jack the TV Ripper</title>
</head>
<body>
<form method="post" action="index.php">
<table>
	<tr>
		<td>Show Title (New)</td>
		<td><input type="text" id="show_title" name="show_title" onkeyup="lookupShow(1,'show_title')" autocomplete="off"> Exact Match<input type="checkbox" name="exact" id="exact" value="1"> </td>

		<td>Show Title (All)</td>
		<td><input type="text" id="show_title_all" name="show_title_all" onkeyup="lookupShow(0,'show_title_all')" autocomplete="off"></td>
	</tr>
	<tr>
		<td colspan=4 align="left">
			<a href="checklength.php">Check Length</a>
		</td>
</table>
<hr />

<span id="results"></span>

</form>
<hr />
<font size="2">Current Series Recordings</font>
<table class="pretty">
	<tr>
		<td><font size="2">Title</font></td>
		<td><font size="2">Display Name</font></td>
		<td><font size="2">Channel</font></td>
		<td><font size="2">Call Sign</font></td>
		<td><font size="2">Delete</font></td>
		<td><font size="2">No TV DB</font></td>
	</tr>
<?php
	$sql = "SELECT * from recording_shows";
	$result = mysql_query($sql,$connect) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		echo "<tr>";
		echo "<td><font size=\"2\">".$row[1]."</font></td>";
		echo "<td><font size=\"2\">".$row[2]."</font></td>";
		echo "<td><font size=\"2\">".$row[3]."</font></td>";
		echo "<td><font size=\"2\">".$row[4]."</font></td>";
		echo "<td><input type=\"button\" name=\"delete_recording\" value=\"Delete Recording\" onClick=\"deleteRecording('".$row[0]."')\"></td>";
		if ($row[5] == 1) {
			echo "<td><input type=\"button\" name=\"no_tv_db\" value=\"Enable TVDB\" onClick=\"notvdb('".$row[0]."')\"></td>";
		} else {
			echo "<td><input type=\"button\" name=\"no_tv_db\" value=\"No TV DB\" onClick=\"notvdb('".$row[0]."')\"></td>";
		}
		echo "</tr>";
	}
?>
</table>
<hr />
<font size="2">Upcoming Shows</font>
<table class="pretty">
        <tr>
                <td><font size="2">Start</font></td>
                <td><font size="2">Filename</font></td>
                <td><font size="2">Channel</font></td>
                <td><font size="2">Title</font></td>
                <td><font size="2">Subtitle</font></td>
                <td><font size="2">TV DB It</font></td>
                <td><font size="2">Rec</font></td>
                <td><font size="2">Manual</font></td>
                <td><font size="2">Descrip</font></td>
                <td><font size="2">Has D</font></td>
                <td><font size="2">Delete</font></td>
        </tr>
<?php
        $sql = "SELECT * from torecord where start >=now() or recording=1 order by start";
        $result = mysql_query($sql,$connect) or die (mysql_error());
        while ($row = mysql_fetch_array($result)) {
		//id,start,end
		$start = $row[1];
		$end = $row[2];
		$start = strtotime($start);
		$end = strtotime($end);
		$len = round(abs($end - $start) / 60,2);
		$bgcolor="#FFFFFF";
		if ($len > 130) {
			$bgcolor="#F5697C";
		}
		echo "<tr bgcolor=\"$bgcolor\">\n";
                echo "<td onClick=\"changeStart('$row[0]','$row[1]')\"><font size=\"2\">".$row[1]." $len</font></td>";
                echo "<td onClick=\"changeName('$row[0]','$row[3]')\"><font size=\"2\">".$row[3]."</font></td>";
                echo "<td><font size=\"2\">".$row[4]."</font></td>";
                echo "<td><font size=\"2\">".$row[6]."</font></td>";
                echo "<td onClick=\"gotopage('$row[3]','$row[1]','$row[0]')\"><font size=\"2\">".$row[5]."</font></td>";
		if ($row[10] == 1) {
			if ($row[11] == 1) {
	                	echo "<td><input type=\"button\" value=\"TVDB Again (TBA)\" onClick=\"tvdbit('$row[3]','$row[1]','$row[0]')\"></td>";
			} else {
	                	echo "<td><input type=\"button\" value=\"TVDB Again\" onClick=\"tvdbit('$row[3]','$row[1]','$row[0]')\"></td>";
			}
		} else {
                	echo "<td><input type=\"button\" value=\"TV DB it\" onClick=\"tvdbit('$row[3]','$row[1]','$row[0]')\"></td>";
		}
		echo "<td align=\"center\">\n";
			if ($row[7] == 1) {
				echo "<img src=\"button_record.jpg\" width=\"15\" height=\"15\"/>";
			} else {
				echo "<img src=\"button_stop.jpg\" width=\"15\" height=\"15\"/>";
			}
		echo "</td>\n";
		echo "<td>\n";
		if ($row[9] == 1) {
			echo "<input type=\"button\" name=\"remove\" value=\"Remove\" onClick=\"makeAuto('$row[0]')\">";
		}	
		echo "</td>\n";	
		echo "<td><input type=\"button\" value=\"Get D\" onClick=\"tvdbdesc('$row[3]','$row[1]','$row[0]')\"></td>";
		echo "<td align=\"center\" onClick=\"hasd('$row[3]','$row[1]','$row[0]','span".$row[0]."')\"><span id=\"span".$row[0]."\"></span></td>\n";
		echo "<td><input type=\"button\" value=\"Delete\" onClick=\"deleteShow('$row[3]','$row[1]','$row[0]')\"></td>";
                echo "</tr>";
        }
?>
</table>
<hr />
<center><img src="jtr.jpg" /> <img src="g1334753559555089220.jpg" /> <img src="Droid_Army_Zoidberg.jpg" /></center>	
</body>
</html>
